from dataclasses import dataclass, field, asdict
from typing import Set, Dict, Any


@dataclass
class Project:
    """用于描述样本分类项目"""
    name: str = ""
    version: int = 0
    location: str = ""
    last_source_path: str = ""
    export_file: str = "category"
    export_sample: str = "sample"
    export_validate: str = "validate"
    sample_type: str = "PNG"
    sample_long_side: int = 512
    training_groups: int = 1
    training_rate: float = 0.8
    validation_rate: float = 0.2
    categories: Set[str] = field(default_factory=set)

    def to_dict(self):
        return asdict(self)

    @classmethod
    def load_from_dict(cls, source: Dict[str, Any]):
        return cls(name=source.get("name", ""),
                   version=source.get("version", 0),
                   location=source.get("location", ""),
                   last_source_path=source.get("last_source_path", ""),
                   export_file=source.get("export_file", "category"),
                   export_sample=source.get("export_sample", "sample"),
                   export_validate=source.get("export_validate", "validate"),
                   sample_type=source.get("sample_type", "PNG"),
                   sample_long_side=source.get("sample_long_side", 512),
                   training_groups=source.get("traning_groups", 1),
                   training_rate=source.get("training_rate", 0.8),
                   validation_rate=source.get("validation_rate", 0.2),
                   categories=set(source.get("categories", [])))
